﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Oracle.DataAccess.Client;

namespace wfaFoxMigrator
{
    public static class CommonFunctions
    {
        public static string GetCurrentCS()
        {
            string testqry = string.Empty;
            switch (Properties.Settings.Default.CurrentDB)
            {
                case "Oracle":
                    testqry = String.Format(Properties.Settings.Default.OracleCS, Properties.Settings.Default.DataSource, Properties.Settings.Default.User, Properties.Settings.Default.Password);
                    break;
                case "SqlServer":
                    testqry = String.Format(Properties.Settings.Default.SqlCS, Properties.Settings.Default.DataSource, Properties.Settings.Default.InitialCatalog, Properties.Settings.Default.User, Properties.Settings.Default.Password);
                    break;
            }
            return testqry;
        }
        public static string GetCurrentCSDest()
        {
            string testqry = string.Empty;
            switch (Properties.Settings.Default.CurrentDBDest)
            {
                case "Oracle":
                    testqry = String.Format(Properties.Settings.Default.OracleCS, Properties.Settings.Default.DataSourceDest, Properties.Settings.Default.UserDest, Properties.Settings.Default.PasswordDest);
                    break;
                case "SqlServer":
                    testqry = String.Format(Properties.Settings.Default.SqlCS, Properties.Settings.Default.DataSourceDest, Properties.Settings.Default.InitialCatalogDest, Properties.Settings.Default.UserDest, Properties.Settings.Default.PasswordDest);
                    break;
            }
            return testqry;
        }  
        public static string GetTestQuery()
        {
            string testqry = string.Empty;
            switch (Properties.Settings.Default.CurrentDB)
            {
                case "Oracle":
                    testqry = Properties.Settings.Default.OracleTestQuery;
                    break;
                case "SqlServer":
                    testqry = Properties.Settings.Default.SqlTestQuery;
                    break;
            }
            return testqry;
        }
        public static bool CheckIfConfigurationWasMade()
        {
            bool answer = false;
            if (!String.IsNullOrEmpty(Properties.Settings.Default.CurrentDB) && 
                !Properties.Settings.Default.CurrentDB.Equals("none") &&
                !String.IsNullOrEmpty(Properties.Settings.Default.InitialPath))
            {
                answer = true;
            }
            return answer;
        }
        public static string GetTypeForDB(Type type,int size)
        {
            string strType = string.Empty;
            string strOraType = string.Empty;
            string strSqlType = string.Empty;
            string valint = string.Empty;
            switch (type.Name.ToLower())
            {
                case "int32":
                    valint = size.Equals(0) ? "19" : size.ToString();
                    strOraType = " NUMBER(" + valint + ")";
                    strSqlType = " BIGINT";
                    break;
                case "string":
                    if (size > 4000) 
                    {                        
                        strOraType = " CLOB";
                    }
                    else
                    {
                        valint = size.Equals(0) ? "255" : size > 1000 ? size.ToString() : size.ToString();
                        strOraType = " VARCHAR2(" + valint + " CHAR)";
                    }                    
                    strSqlType = " VARCHAR(MAX)";
                    break;
                case "decimal":
                    /*valint = size.Equals(0) ? "19,4" : size.ToString() + ",4";
                    strOraType = " NUMBER(" + valint + ")";*/
                    strOraType = " NUMBER(19,4)";
                    strSqlType = " DECIMAL";
                    break;
                case "boolean":
                    valint = size.Equals(0) ? "255" : size.ToString();
                    strOraType = " VARCHAR2(1 CHAR)";
                    strSqlType = " VARCHAR(MAX)";
                    break;
                case "datetime":
                    strOraType = " DATE";
                    strSqlType = " DATETIME";
                    break;
                case "byte[]":
                    strOraType = "BLOB";
                    strSqlType = "VARBINARY";
                    break;
                default:
                    if (size > 4000) 
                    {                        
                        strOraType = " CLOB";
                    }
                    else
                    {
                        valint = size.Equals(0) ? "255" : size.ToString();
                        strOraType = " VARCHAR2(" + valint + " CHAR)";
                    }                    
                    strSqlType = " VARCHAR(MAX)";
                    break;
            }
            switch (Properties.Settings.Default.CurrentDB)
            {
                case "Oracle":
                    strType = strOraType;
                    break;
                case "SqlServer":
                    strType = strSqlType;
                    break;
            }
            return strType;
        }
        public static string AnalizeColumnsAndTranslateSQL(DataTable dt)
        {
            string strSql = string.Empty;
            try
            {
                dt.Columns.Cast<DataColumn>().ToList().ForEach(delegate(DataColumn dc)
                {
                    //strSql += " " + dc.ColumnName + ", ";
                    switch (dc.DataType.Name.ToLower())
                    {
                    //    //IIF([Meals].[TempStop] Is Null, Null, [Meals].[TempStop]) 
                        case "decimal":
                            //strSql += "IIF(ISNULL(" + dc.ColumnName + ", 0, CAST(" + dc.ColumnName + " as numeric(11,4))) as '" + dc.ColumnName + "', ";
                            //strSql += "IIF(ISNULL(" + dc.ColumnName + ") , 0," + dc.ColumnName + ") as '" + dc.ColumnName + "', ";
                            //strSql += "VAL(STR(" + dc.ColumnName + ")) as '" + dc.ColumnName + "', ";
                            strSql += " " + dc.ColumnName + ", ";
                            break;
                        default:
                            strSql += " " + dc.ColumnName + ", ";
                            break;
                    }
                });
                if (strSql.LastIndexOf(", ") == strSql.Length - 2)
                {
                    strSql = strSql.Substring(0, strSql.Length - 2);
                    strSql = String.Format(Properties.Settings.Default.SelectClause, strSql, dt.TableName);
                }
            }
            catch (Exception ex)
            {
                strSql = String.Format(Properties.Settings.Default.InvalidTableMsg, dt.TableName, ex.Message) + Environment.NewLine;
            }
            return strSql;
        }
        public static bool CheckIfItsJetEnabled()
        {
            return Properties.Settings.Default.UseJetProvider;
        }
        public static OracleDbType getOracleType(Type type, int size)
        {
            OracleDbType curType = OracleDbType.Varchar2;
            switch (type.Name.ToLower())
            {
                case "string":
                    if (size > 3999)
                    {
                        curType = OracleDbType.Clob;
                    }
                    else
                    {
                        curType = OracleDbType.Varchar2;
                    }
                    break;
                case "decimal":
                    curType = OracleDbType.Decimal;
                    break;
                case "datetime":
                    curType = OracleDbType.Date;
                    break;      
                case "byte":
                    curType = OracleDbType.Blob;
                    break;      
            }
            return curType;
        }
    }
}
